package com.zxw.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxw.common.web.util.JwtUtils;
import com.zxw.flow.domain.entity.FlowBasicInfo;
import com.zxw.flow.domain.entity.FlowNodeInfo;
import com.zxw.flow.domain.entity.FlowType;
import com.zxw.flow.domain.vo.FlowNodeInfoPageVO;
import com.zxw.flow.domain.vo.FlowNodeInfoVO;
import com.zxw.flow.mapper.FlowNodeInfoMapper;
import com.zxw.flow.service.FlowNodeInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Xuewei.Zhang
 * @since 2022-07-04
 */
@Service
@RequiredArgsConstructor
public class FlowNodeInfoServiceImpl extends ServiceImpl<FlowNodeInfoMapper, FlowNodeInfo> implements FlowNodeInfoService {

    private final FlowNodeInfoMapper flowNodeInfoMapper;

    @Override
    public IPage<FlowNodeInfo> getPage(FlowNodeInfoPageVO pageVO) {
        Page<FlowNodeInfo> page = new Page<>(pageVO.getPageNum(), pageVO.getPageSize());
        LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<FlowNodeInfo>()
                .eq(FlowNodeInfo::getCompanyId, pageVO.getCompanyId())
                .eq(null != pageVO.getApproveResult(), FlowNodeInfo::getApproveResult, pageVO.getApproveResult())
                .eq(null != pageVO.getApproveState(), FlowNodeInfo::getApproveState, pageVO.getApproveState());

        IPage<FlowNodeInfo> data = flowNodeInfoMapper.selectPage(page, wrapper);
        return data;
    }

    @Override
    public FlowNodeInfoVO add(FlowNodeInfoVO flowNodeInfoVO) {
        flowNodeInfoMapper.insert(flowNodeInfoVO);
        return flowNodeInfoVO;
    }

    @Override
    public FlowNodeInfo getFlowNodeInfoById(Long id) {
        Long companyId = JwtUtils.getCompanyId();
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper<FlowNodeInfo>()
                .eq(FlowNodeInfo::getId, id)
                .eq(FlowNodeInfo::getCompanyId, companyId);
        return flowNodeInfoMapper.selectOne(wrapper);
    }

    @Override
    public void updateBase(FlowNodeInfoVO flowNodeInfoVO) {
        flowNodeInfoMapper.update(flowNodeInfoVO, new LambdaUpdateWrapper<FlowNodeInfo>()
                .eq(FlowNodeInfo::getId, flowNodeInfoVO.getId())
                .eq(FlowNodeInfo::getCompanyId, flowNodeInfoVO.getCompanyId())
        );
    }

    @Override
    public void delete(String flowNodeInfoIds) {
        Long companyId = JwtUtils.getCompanyId();
        flowNodeInfoMapper.delete(new LambdaUpdateWrapper<FlowNodeInfo>()
                .eq(FlowNodeInfo::getCompanyId, companyId)
                .in(FlowNodeInfo::getId, flowNodeInfoIds)
        );
    }
}
